package com.weibo.basic.alg.alert.server.controller;

import com.weibo.basic.alg.alert.common.entity.JsonReturn;
import com.weibo.basic.alg.alert.common.entity.SendMessageRequest;
import com.weibo.basic.alg.alert.server.config.Constants;
import com.weibo.basic.alg.alert.server.entity.Alert;
import com.weibo.basic.alg.alert.server.service.IAlertService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(Constants.PATH_PREFIX)
@Slf4j
public class AlertController {

    @Autowired
    private IAlertService alertService;

    @RequestMapping(value = "/alert/", method = RequestMethod.POST)
    @ApiOperation(value = "报警")
    public JsonReturn<Boolean> alert(@RequestBody Alert alert) {
        log.info("alert msg is {}", alert);
        try {
            if (alertService.alert(alert)) {
                return JsonReturn.returnSuccess(null);
            } else {
                return JsonReturn.returnFailed("send failed");
            }
        } catch (Exception ex) {
            return JsonReturn.returnFailed(ex.getMessage());
        }

    }

    @RequestMapping(value = "/sendMsg", method = RequestMethod.POST)
    @ApiOperation(value = "下发各种通知信息")
    public JsonReturn<Boolean> sendMsg(@RequestBody SendMessageRequest sendMessageRequest) {

        log.info("send msg is {}", sendMessageRequest);
        try {
            if (alertService.alert(sendMessageRequest)) {
                return JsonReturn.returnSuccess(null);
            } else {
                return JsonReturn.returnFailed("send failed");
            }
        } catch (Exception ex) {
            return JsonReturn.returnFailed(ex.getMessage());
        }
    }

}
